package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.*;
import com.sg.entity.OmsEvaluationSubject;
import com.sg.service.base.app.MOmsEvaluationSubjectService;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.OmsEvaluationSubjectMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsEvaluationSubjectServiceImpl
        extends ServiceImpl<OmsEvaluationSubjectMapper, OmsEvaluationSubject>
        implements MOmsEvaluationSubjectService {

    @Autowired
    OmsEvaluationSubjectMapper omsEvaluationSubjectMapper;

    /**
     * 3-2-02批量查询评价主题列表[2144]
     * gen by moon at 10/10/2022, 9:56:41 PM
     */
    @Trace(operationName = "3-2-02批量查询评价主题列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsEvaluationSubject> batchQueryEvaThemeList(BatchQueryEvaThemeListReq reqDto) {

        try {
            QueryWrapper<OmsEvaluationSubject> queryWrapper = new QueryWrapper<>();

            if (reqDto.getEvalThemeList() == null || reqDto.getEvalThemeList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("evaluation_subject_id", reqDto.getEvalThemeList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsEvaluationSubject.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsEvaluationSubject.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsEvaluationSubject.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsEvaluationSubject>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-2-01批量查询评价主题[3090]
     * gen by moon at 10/10/2022, 9:56:43 PM
     */
    @Trace(operationName = "3-2-01批量查询评价主题")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsEvaluationSubject> batchQueryEvaTheme(BatchQueryEvaThemeReq reqDto) {

        try {
            QueryWrapper<OmsEvaluationSubject> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getEvalThemeList() == null || reqDto.getEvalThemeList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("evaluation_subject_code", reqDto.getEvalThemeList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsEvaluationSubject.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsEvaluationSubject.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsEvaluationSubject.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsEvaluationSubject>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-2-01查评价主题详情[2310]
     * gen by moon at 10/10/2022, 9:56:43 PM
     */
    @Trace(operationName = "3-2-01查评价主题详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsEvaluationSubject queryEvaThemeDetail(QueryEvaThemeDetailReq reqDto) {

        try {

            QueryWrapper<OmsEvaluationSubject> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsEvaluationSubject> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsEvaluationSubject retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-2-01修改评价主题[2280]
     * gen by moon at 10/10/2022, 9:56:42 PM
     */
    @Trace(operationName = "3-2-01修改评价主题")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateEvaTheme(OmsEvaluationSubject omsEvaluationSubject) {
        omsEvaluationSubject.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsEvaluationSubject != null && omsEvaluationSubject.getSubjectLifeCycle() != null && omsEvaluationSubject.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsEvaluationSubject.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsEvaluationSubject != null && omsEvaluationSubject.getSubjectLifeCycle() != null && omsEvaluationSubject.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsEvaluationSubject.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsEvaluationSubject.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsEvaluationSubject != null && omsEvaluationSubject.getSubjectLifeCycle() != null && omsEvaluationSubject.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsEvaluationSubject.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (omsEvaluationSubject != null && omsEvaluationSubject.getSubjectLifeCycle() != null && omsEvaluationSubject.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsEvaluationSubject.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(omsEvaluationSubject);
    }

    /**
     * 3-2-01查评价主题列表[2278]
     * gen by moon at 10/10/2022, 9:56:41 PM
     */
    @Trace(operationName = "3-2-01查评价主题列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsEvaluationSubject> queryEvaThemeList(QueryEvaThemeListReq reqDto) {

        try {
            QueryWrapper<OmsEvaluationSubject> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsEvaluationSubject.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsEvaluationSubject.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsEvaluationSubject.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsEvaluationSubject>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-2-02新增评价主题[2117]
     * gen by moon at 5/24/2023, 3:41:12 AM
     */
    @Trace(operationName = "3-2-02新增评价主题")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addEvaTheme(OmsEvaluationSubject omsEvaluationSubject) {
        omsEvaluationSubject.setEvaluationSubjectId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsEvaluationSubject.setEvaluationSubjectCode(CommonFunctionHelper.getUid());//AUTO_GENERATE
        omsEvaluationSubject.setIsArchive("FALSE");
        omsEvaluationSubject.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsEvaluationSubject.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsEvaluationSubject != null && omsEvaluationSubject.getSubjectLifeCycle() != null && omsEvaluationSubject.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsEvaluationSubject.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsEvaluationSubject.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsEvaluationSubject.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsEvaluationSubject != null && omsEvaluationSubject.getSubjectLifeCycle() != null && omsEvaluationSubject.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsEvaluationSubject.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsEvaluationSubject.setIsValid("FALSE");
        omsEvaluationSubject.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsEvaluationSubject.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsEvaluationSubject)) {
            return omsEvaluationSubject.getEvaluationSubjectId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-2-01删除评价主题[3163]
     * gen by moon at 10/10/2022, 9:56:44 PM
     */
    @Trace(operationName = "3-2-01删除评价主题")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteEvaTheme(String evaluationSubjectId) {

        return super.removeById(evaluationSubjectId);
    }

    /**
     * M3-2构造单条评价主题验证数据[4263]
     * gen by moon at 10/10/2022, 9:56:45 PM
     */
    @Trace(operationName = "M3-2构造单条评价主题验证数据")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String buildSingleEvaSubject(OmsEvaluationSubject omsEvaluationSubject) {
        omsEvaluationSubject.setEvaluationSubjectId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsEvaluationSubject.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsEvaluationSubject.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsEvaluationSubject != null && omsEvaluationSubject.getSubjectLifeCycle() != null && omsEvaluationSubject.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsEvaluationSubject.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsEvaluationSubject != null && omsEvaluationSubject.getSubjectLifeCycle() != null && omsEvaluationSubject.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsEvaluationSubject.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsEvaluationSubject.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsEvaluationSubject.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsEvaluationSubject != null && omsEvaluationSubject.getSubjectLifeCycle() != null && omsEvaluationSubject.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsEvaluationSubject.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (omsEvaluationSubject != null && omsEvaluationSubject.getSubjectLifeCycle() != null && omsEvaluationSubject.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsEvaluationSubject.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        omsEvaluationSubject.setIsValid("FALSE");
        if (super.save(omsEvaluationSubject)) {
            return omsEvaluationSubject.getEvaluationSubjectId();
        } else {
            return "-1";
        }

    }

    /**
     * M3-2查询单条评价主题详情[4270]
     * gen by moon at 10/10/2022, 9:56:45 PM
     */
    @Trace(operationName = "M3-2查询单条评价主题详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsEvaluationSubject querySingleEvaSubjectDetail(QuerySingleEvaSubjectDetailReq reqDto) {

        try {
            reqDto.setAscriptionSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsEvaluationSubject> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsEvaluationSubject> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsEvaluationSubject retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
